// Pipit JSON处理模块
#include "pipit_stdlib.h"
#include <ctype.h>

// JSON值类型
typedef enum {
    JSON_NULL,
    JSON_BOOL,
    JSON_NUMBER,
    JSON_STRING,
    JSON_ARRAY,
    JSON_OBJECT
} json_type_t;

// JSON值结构
typedef struct json_value {
    json_type_t type;
    union {
        int bool_value;
        double number_value;
        char* string_value;
        struct json_array* array_value;
        struct json_object* object_value;
    } value;
} json_value_t;

// JSON数组
typedef struct json_array {
    json_value_t** items;
    size_t count;
    size_t capacity;
} json_array_t;

// JSON对象
typedef struct json_object {
    char** keys;
    json_value_t** values;
    size_t count;
    size_t capacity;
} json_object_t;

// 创建JSON值
json_value_t* json_create_null() {
    json_value_t* val = malloc(sizeof(json_value_t));
    val->type = JSON_NULL;
    return val;
}

json_value_t* json_create_bool(int value) {
    json_value_t* val = malloc(sizeof(json_value_t));
    val->type = JSON_BOOL;
    val->value.bool_value = value;
    return val;
}

json_value_t* json_create_number(double value) {
    json_value_t* val = malloc(sizeof(json_value_t));
    val->type = JSON_NUMBER;
    val->value.number_value = value;
    return val;
}

json_value_t* json_create_string(const char* value) {
    json_value_t* val = malloc(sizeof(json_value_t));
    val->type = JSON_STRING;
    val->value.string_value = strdup(value);
    return val;
}

// 创建JSON数组
json_array_t* json_array_create() {
    json_array_t* arr = malloc(sizeof(json_array_t));
    arr->items = NULL;
    arr->count = 0;
    arr->capacity = 0;
    return arr;
}

void json_array_append(json_array_t* arr, json_value_t* value) {
    if (arr->count >= arr->capacity) {
        arr->capacity = arr->capacity == 0 ? 4 : arr->capacity * 2;
        arr->items = realloc(arr->items, arr->capacity * sizeof(json_value_t*));
    }
    arr->items[arr->count++] = value;
}

// 创建JSON对象
json_object_t* json_object_create() {
    json_object_t* obj = malloc(sizeof(json_object_t));
    obj->keys = NULL;
    obj->values = NULL;
    obj->count = 0;
    obj->capacity = 0;
    return obj;
}

void json_object_set(json_object_t* obj, const char* key, json_value_t* value) {
    for (size_t i = 0; i < obj->count; i++) {
        if (strcmp(obj->keys[i], key) == 0) {
            free(obj->values[i]);
            obj->values[i] = value;
            return;
        }
    }
    
    if (obj->count >= obj->capacity) {
        obj->capacity = obj->capacity == 0 ? 4 : obj->capacity * 2;
        obj->keys = realloc(obj->keys, obj->capacity * sizeof(char*));
        obj->values = realloc(obj->values, obj->capacity * sizeof(json_value_t*));
    }
    
    obj->keys[obj->count] = strdup(key);
    obj->values[obj->count] = value;
    obj->count++;
}

// 简单JSON解析（基础版本）
char* json_serialize_string(const char* str) {
    size_t len = strlen(str);
    char* result = malloc(len * 2 + 3); // 最坏情况：所有字符都需要转义 + 引号
    char* p = result;
    *p++ = '"';
    
    while (*str) {
        switch (*str) {
            case '"': *p++ = '\\'; *p++ = '"'; break;
            case '\\': *p++ = '\\'; *p++ = '\\'; break;
            case '\n': *p++ = '\\'; *p++ = 'n'; break;
            case '\r': *p++ = '\\'; *p++ = 'r'; break;
            case '\t': *p++ = '\\'; *p++ = 't'; break;
            default: *p++ = *str; break;
        }
        str++;
    }
    
    *p++ = '"';
    *p = '\0';
    return result;
}